//封装分类业务相关代码
import { getCategoryAPI } from "@/apis/category"
import { onMounted, ref } from "vue"
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'

export function useCategory() {
    //获取分类数据
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
        console.log(res);
    }

    onMounted(() => {
        getCategory()
    })

    //路由参数变化的时候，把分类数据接口重新发送
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })

    return{
        categoryData
    }
}